package com.dpwgc.demo.controller;

import com.dpwgc.demo.model.IndexData;
import com.dpwgc.demo.service.AdminService;
import com.dpwgc.demo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * 管理员
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/Admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Value("${sftp.rootPath}")
    String sftpRootPath;

    @Value("${file.storage.path}")
    String fileStoragePath;

    //文件上传大小限制
    @Value("${spring.servlet.multipart.max-file-size}")
    String maxFileSize;

    //文件下载大小限制
    @Value("${spring.servlet.multipart.max-request-size}")
    String maxRequestSize;

    //SFTP服务器IP地址
    @Value("${sftp.hostname}")
    String sftpHostname;

    //SFTP服务器端口号
    @Value("${sftp.port}")
    String sftpPort;

    /**
     * 管理员登录
     * @param username 用户名
     * @param password 密码
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResultUtil<Object> login(@RequestParam("username") String username,
                                    @RequestParam("password") String password){

        return adminService.loginService(username,password);

    }

    /**
     * 获取文件存储基本配置信息
     * @return
     */
    @RequestMapping(value = "/getData",method = RequestMethod.POST)
    public ResultUtil<Object> getData(){

        ResultUtil<Object> resultUtil = new ResultUtil<>();

        try{
            IndexData indexData = new IndexData();

            indexData.setRootPath(sftpRootPath);
            indexData.setMaxFileSize(maxFileSize);
            indexData.setMaxRequestSize(maxRequestSize);
            indexData.setSftpHostname(sftpHostname);
            indexData.setSftpPort(sftpPort);

            resultUtil.setCode(200);
            resultUtil.setMsg("获取成功");
            resultUtil.setData(indexData);
            return resultUtil;

        }catch (Exception e){

            resultUtil.setCode(100);
            resultUtil.setMsg("获取失败");
            resultUtil.setData(e);
            return resultUtil;
        }
    }

    /**
     * 根据logUri获取访问记录列表
     * @param logUri 访问控制器路径,例：/Upload/uploadFile
     * @param startPage 起始页
     * @param endPage 从起始页开始计数——计到第endPage页结束
     * @return
     */
    @RequestMapping(value = "/queryLogByUri",method = RequestMethod.POST)
    public ResultUtil<Object> queryLogByUri(@RequestParam("logUri") String logUri,
                                            @RequestParam("startPage") Integer startPage,
                                            @RequestParam("endPage") Integer endPage){

        return adminService.queryLogByUriService(logUri,startPage,endPage);

    }

    /**
     * 获取文件列表
     * @param directory 文件目录
     * @return
     */
    @RequestMapping(value = "/fileList",method = RequestMethod.POST)
    public ResultUtil<Object> fileList(@RequestParam(value = "directory") String directory){

        return adminService.fileListService(sftpRootPath+directory);
    }

    /**
     * 删除文件
     * @param directory  要删除文件所在目录
     * @param deleteFile 要删除的文件
     */
    @RequestMapping(value = "/deleteFile",method = RequestMethod.POST)
    public ResultUtil<Object> deleteFile(@RequestParam(value = "directory") String directory,
                                         @RequestParam(value = "deleteFile") String deleteFile){

        return adminService.deleteFileService(sftpRootPath+directory, deleteFile);
    }

    /**
     * 压缩 zip
     * @param filePath  文件夹 全路径(例：/file/test/)
     * @param fileName  文件夹名称(例：test)
     * @param outPath   压缩文件保存路径(例：/file/zip/test.zip)
     */
    @RequestMapping(value = "/zipFile",method = RequestMethod.POST)
    public ResultUtil<Object> zipFile(@RequestParam("filePath") String filePath,
                                      @RequestParam("fileName") String fileName,
                                      @RequestParam("outPath") String outPath) throws IOException {

        return adminService.zipFileService(fileStoragePath+filePath, fileName, fileStoragePath+outPath);

    }

    /**
     * 下载文件(网页端不可用)
     *
     * @param directory    下载目录
     * @param downloadFile 下载的文件
     * @param saveFile     存在本地的路径
     */
    /*
    @RequestMapping(value = "/downloadFile",method = RequestMethod.POST)
    public ResultUtil<Object> downloadFile(@RequestParam(value = "directory") String directory,
                                           @RequestParam(value = "downloadFile") String downloadFile,
                                           @RequestParam(value = "saveFile") String saveFile){

        return adminService.downloadFileService(rootPath+directory, downloadFile, saveFile);
    }
     */
}
